package com.jf.yytgd.validation.validator;

import com.jf.yytgd.validation.constrain.Phone;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import java.math.BigDecimal;

/**
 * Created with IntelliJ IDEA
 * Description: Phone验证逻辑实现
 * User:22954
 * Date:2025-01-14
 * Time:0:12
 */
public class PhoneValidator implements ConstraintValidator<Phone, Object> {
    private static final String regex = "^[0-9]{7,11}$";

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        if (value == null) {
            return false;
        }
        if (value instanceof String phone) {
            return phone.matches(regex);
        }
        if (value instanceof BigDecimal phone) {
            return phone.compareTo(new BigDecimal("1000000")) >= 0 && phone.compareTo(new BigDecimal("99999999999")) <= 0;
        }
        return false;
    }
}
